// import { IMediator } from './mediatorProps'

// 返回一个对象
// const mediator = (function() {
//   const topicList = {} as { [key: string]: any }
//   let uuid = 0

//   function subscribe(topic:string, callback: (...args:unknown[]) => void) {
//     uuid++
//     topicList[topic] = topicList[topic]
//     ? [...topicList[topic], { callback, uuid }]
//     : [{ callback, uuid }]
//   }

//   function publish(topic:string, ...args:unknown[]) {
//      if (topicList[topic]) {
//         topicList[topic].map((item:any) => item.callback(args))
//      }
//   }

//   return {
//     install: function(obj:IMediator) {
//       obj.uid = uuid
//       obj.publish = publish
//       obj.subscribe = subscribe
//     }
//   }
// })()

// export default mediator

export class Mediator {
  topicList = {} as { [key: string]: any }
  uuid = 0
  constructor() {
    console.log(`mediator初始化...`)
    console.log(`this.topicList`, this.topicList)
    console.log(`this.uuid`, this.uuid)
  }

  subscribe(topic: string, callback: (...args: unknown[]) => void) {
    const uuid = this.uuid++
    this.topicList[topic] = this.topicList[topic]
      ? [...this.topicList[topic], { callback, uuid }]
      : [{ callback, uuid }]
  }

  publish(topic: string, ...args: unknown[]) {
    if (this.topicList[topic]) {
      this.topicList[topic].map((item: any) => item.callback(args))
    }
  }
}

export default Mediator
